package example.dao;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import example.pojo.Temperature;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ConnectionMysql {
    public static Connection getConn() {
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/car";
        String username = "root";
        String password = "Songmingyu123@";
        Connection conn = null;
        try {
            Class.forName(driver); //classLoader,加载对应驱动
            conn = (Connection) DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    public static int insert(Temperature tmp) {
        Connection conn = getConn();
        int i = 0;
        String sql = "insert into students (Name,Sex,Age) values(?,?,?)";
        PreparedStatement pstmt;
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            pstmt.setInt(1, tmp.getId());
            pstmt.setInt(2, tmp.getTemp());
            pstmt.setString(3, tmp.getDate());
            i = pstmt.executeUpdate();
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    public static String getAll() {
        String temperatureInfo="";
        Connection conn = getConn();
        String sql = "select * from carinfo";
        PreparedStatement pstmt;
        try {
            pstmt = (PreparedStatement)conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            int col = rs.getMetaData().getColumnCount();
            System.out.println("============================");
            while (rs.next()) {
                for (int i = 2; i <= col; i++) {
                    if(i==2){
                        System.out.println(">>>"+rs.getString(i));
                        String[] sourceStrArray = rs.getString(i).split(" ");
                        temperatureInfo+=sourceStrArray[1]+",";
                    }else if(i==3){
                        temperatureInfo+=rs.getString(i);
                    }
                    System.out.println("IIIIII");
                    System.out.print(rs.getString(i) + "\t");
                    if ((i == 2) && (rs.getString(i).length() < 8)) {
                        System.out.print("\t");
                    }
                }
                temperatureInfo+="\n";
                System.out.println("\n");
            }
            System.out.println("============================");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return temperatureInfo;
    }

    public static int getOne() {
        String temperatureInfo="";
        int tmp=0;
        Connection conn = getConn();
        String sql = "SELECT * FROM carinfo where used=0 LIMIT 0,1;";
        PreparedStatement pstmt;
        try {
            pstmt = (PreparedStatement)conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            int col = rs.getMetaData().getColumnCount();
            System.out.println("============================");
            while (rs.next()) {
//                for (int i = 2; i <= col; i++) {
//                    if(i==2){
//                        System.out.println(">>>"+rs.getString(i));
//                        String[] sourceStrArray = rs.getString(i).split(" ");
//                        temperatureInfo+=sourceStrArray[1]+",";
//                    }else if(i==3){
//                        temperatureInfo+=rs.getString(i);
//                    }
//                    System.out.println("IIIIII");
//                    System.out.print(rs.getString(i) + "\t");
//                    if ((i == 2) && (rs.getString(i).length() < 8)) {
//                        System.out.print("\t");
//                    }
//                }
//                temperatureInfo+="\n";
//                System.out.println("\n");
                tmp=rs.getInt(3);
                int id=rs.getInt(1);
                update(id);
            }
            System.out.println("============================");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return tmp;
    }

    public static int update(int id) {
        Connection conn = getConn();
        int i = 0;
        String sql = "update carinfo set used=1 where id='"+id+"';";
        PreparedStatement pstmt;
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            i = pstmt.executeUpdate();
            System.out.println("resutl: " + i);
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }
}
